<?php
declare (strict_types = 1);

namespace app\api\model;
use app\api\model\Student as ModelStudent;
use app\api\model\Course as ModelCourse;
use app\api\model\Stage as ModelStage;
use app\api\model\Week as ModelWeek;
use app\api\model\Chapter as ModelChapter;
use app\api\model\StudentChapterRelation as ModelStudentChapterRelation;
use app\api\service\UserToken;
use app\api\model\StudentStageRelation as ModelStudentStageRelation;

/**
 * 课程模型
 */
class StuCourse extends Base
{
    protected $autoWriteTimestamp = 'datetime';
    /**
     * 
     * 获取课程
     */
    public static function getStuCourse($uid)
    {
        $student = ModelStudent::find($uid); 
        if(empty($student)){
           return [
               'code' => 400,
               'message' => '没有该学生课程信息'
           ];
        }
        $course_id = $student['course_id'];
        $data = ModelCourse::where('id',$course_id)->field('id,name')->find(); //课程名称

        $courseCount = StuCourse::getChapterCount($course_id); //课程门数
        $stuCount = count(ModelStudent::where('course_id',$course_id)->field('id')->select());//学生人数

        $data['courseCount'] = $courseCount;
        $data['stuCount'] = $stuCount;
        return [
            'code' => 200,
            'message' => '获取数据成功',
            'data' => $data,
        ];
    } 

    /**
     * 
     * 获取单个阶段下课程
     */
   
    public static function getStudentWeekCourse($stage_id)
    {
        $uid =  UserToken::getCurrentUid();
        $status = ModelStudentStageRelation::where([
            'student_id' => $uid,
            'stage_id' => $stage_id
        ])->find();
        if(empty($status) || $status->is_lock != 0){
            return [
                'code' => 400,
                'message' => '课程未解锁'
            ];
        }
        $weeks = ModelWeek::where([
            'stage_id' => $stage_id,
            'is_online' => 0
        ])->field('id,title,week_name')->order('week_name')->select()->toArray();

        if(empty($weeks)){
            return [
                'code' => 200,
                'message' => '信息获取成功',
                'data' => []
            ];
        }
       
        StuCourse::getChapter($weeks);
        return [
            'code' => 200,
            'message' => '信息获取成功',
            'data' => $weeks
        ];
    }
    
    /**
     * 
     * 获取课程所有信息
     */

    public static function getStuCourseAllInfo($course_id,$stage)
    {
        $data = StuCourse::getStage($course_id,$stage);
        return $data;
    }

    /**
     * 
     * 获取阶段信息
     */
    protected static function getStage($course_id,$stage)
    {
        $data = ModelStage::where('course_id',$course_id)->field('id,stage')->select()->toArray();
        if(empty($data)){
            return [
                'code' => 200,
                'message' => '数据获取成功',
                'data' => $data
            ];
        }
        ModelStage::getStageName($data);
        StuCourse::getStageStatus($data);
        StuCourse::getWeek($data,$stage);
        
        return $data;
    }



    /**
     * 
     *  获取阶段状态
     */
    protected static function getStageStatus(&$data)
    {
        $uid =  UserToken::getCurrentUid();
        foreach($data as $key=>$stage){
            $stageStatus = ModelStudentStageRelation::where([
                'student_id' => $uid,
                'stage_id' => $stage['id']
            ])->find(); 
            if(!empty($stageStatus)){
                $data[$key]['is_lock'] = $stageStatus['is_lock'];
            }else{
                $data[$key]['is_lock'] = 1;
            }
        }
    }

    /**
     * 
     * 获取周期信息
     */
    protected static function getWeek(&$data,$stageNumber)
    {
        foreach($data as $key=>$stage)
        {

            if($stage['stage'] == $stageNumber){
                $weeks = ModelWeek::where([
                    'stage_id' => $stage['id'],
                    'is_online' => 0
                ])->field('id,title,week_name')->order('week_name')->select()->toArray();
                if(!empty($weeks)){
                    StuCourse::getChapter($weeks);
                    $data[$key]['week'] = $weeks;
                }else{
                    $data[$key]['week'] = $weeks;
                    $data[$key]['week']['chapter'] = []; 
                } 
            }
        }
    }


    /**
     * 
     * 获取章节信息
     */
    protected static function getChapter(&$weeks)
    {
       
        foreach($weeks as $key=>$week){
            $chapter = ModelChapter::where([
                'week_id' => $week['id'],
                'is_lock' => 0
            ])->field('id,title')->select();
            if(! empty($chapter)){
                StuCourse::getChapterStatus($chapter);
                $weeks[$key]['chapter'] = $chapter;
            }else{
                $weeks[$key]['chapter'] = [];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
            }
            
        } 
        
    }

    /**
     * 
     * 获取章节状态
     */

    protected static function getChapterStatus(&$cha)
    {
        $uid =  UserToken::getCurrentUid();
        foreach($cha as $key=>$arr){
            $status = ModelStudentChapterRelation::where([
                'student_id' => $uid,
                'chapter_id' => $arr['id']
            ])->field('is_lock,is_complete')->find()->toArray();

            if(empty($status)){
                $cha[$key]['is_lock'] =  1;
                $cha[$key]['is_complete'] =  1;
            }else{
                $cha[$key]['is_lock'] =  $status['is_lock'];
                $cha[$key]['is_complete'] =  $status['is_complete'];
            }
            
        }
    }


    /**
     * 
     *  计算课程数
     */

    protected static function getChapterCount($course_id)
    {
        $stage_ids = ModelStage::where('course_id',$course_id)->field('id')->select()->toArray();
        if(empty($stage_ids)){
            return 0;
        }
        $weeks = [];
        foreach($stage_ids as $key=>$stage){
            $week_ids = ModelWeek::where('stage_id',$stage['id'])->field('id')->select()->toArray();
            if(! empty($week_ids)){
                foreach($week_ids as $key=>$week){
                    $weeks[] = $week_ids[$key];
                }
            }
        }
        if(count($weeks) <= 0){
            return 0;
        }
        $count = 0;
        foreach($weeks as $key=>$week){
            $cha = ModelChapter::where('week_id',$week['id'])->field('id')->select();
            $count += count($cha);
        }
        return $count;
    }
}